<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            position: relative;
        }
        #box{
            height: 400px;
            width: 400px;
            border: 5px solid olive;
            position: relative;
        }
        #bigBox{
            height: 300px;
            width: 300px;
            position: absolute;
            border: 2px solid red;
            left: 500px;
            top: 0;
            overflow: hidden;
        }
        #box img{
            width: 100%;
            height: 100%;
            
        }
        #lay{
           
            background-color: #ffffff;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0.4;
            display: none;
        }
    </style>
</head>
<body>
    <div id="box">
        <img src="img/13.jpg" alt="text">
        <span id="lay"></span>
    </div>
    <div id="bigBox">
        <img src="img/11.jpg" alt="text">
    </div>
<script>
       var box = document.getElementById('box');
       var bigbox = document.getElementById('bigBox');
       var bigbox1 = bigbox.children[0];

       var lay = document.getElementById('lay');

       box.onmouseover = function(){
           bigbox.style.display = "block";
           lay.style.display = "block";
       }
       box.onmouseout = function(){
           bigbox.style.display = "none";
           lay.style.display = "none";
       }
       box.onmousemove = function(e){
           e = e || event;
           var scale = 4;

           var x = e.clientX - lay.offsetWidth / 2;
           var y = e.clientY- lay.offsetHeight / 2;
        //    console.log(x);
           lay.style.width = parseInt(box.offsetWidth / scale) + "px";
           lay.style.height = parseInt(box.offsetHeight / scale) + "px";
            
            bigbox1.style.width = box.offsetWidth * scale + "px";
            bigbox1.style.height = box.offsetHeight * scale + "px";

            if(x < 0){
                x = 0;
            }else if(x >= box.offsetWidth - lay.offsetWidth){
                x = box.offsetWidth - lay.offsetWidth;
            }

            if(y < 0){
                y = 0;
            }else if(y >= box.offsetHeight - lay.offsetHeight){
               y = box.offsetHeight - lay.offsetHeight;
            }
            lay.style.left = x + "px";
            lay.style.top = y + "px";

            var left = lay.offsetLeft * scale;
            var top = lay.offsetTop * scale;
            bigbox1.style.marginLeft = (left * (-1)) + 'px';
            bigbox1.style.marginTop = (top * (-1)) + 'px';
       }
</script>

</body>
</html>